package com.adminx.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.adminx.entity.Authority;
import com.adminx.pojo.AuthorityPojo;
import com.adminx.pojo.NavigationMenuPojo;
import com.adminx.security.MyAuthResoMapping;
import com.adminx.service.AuthorityService;
import com.adminx.service.MyUserDetailsService;
import com.adminx.service.ResourceService;
import com.adminx.util.Constant;
import com.adminx.util.PageResults;


@Controller
@RequestMapping("authority")
public class AuthorityController {
	
	@Autowired
	private AuthorityService authorityService;
	
	@Autowired
	private ResourceService resourceService;
	
	@Autowired
	private MyUserDetailsService userDetailsService;
	
	/**
	 * 初始化权限管理页�?
	 * @param map
	 * @return 
	 */
	@RequestMapping(value = "/init", method = RequestMethod.GET) 
	public String findAllUser(ModelMap map){
		NavigationMenuPojo navigationMenuPojo = this.resourceService.queryNavigation("/authority/init.html");
		map.put("navigationMenuPojo", navigationMenuPojo);
		map.put("url", "/authority/init.html");
		return "admin/authority/authority";
	}
	
	/**
	 * 分页查询权限
	 * @param map
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/query", method = RequestMethod.GET) 
	public Map<String, Object> query(Integer pageIndex, Integer pageSize, String searchText){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			PageResults<AuthorityPojo> result = this.authorityService.query(pageIndex, pageSize, searchText);
			map.put("result", result);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 查询权限
	 * @param map
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/findAuthorityList", method = RequestMethod.GET) 
	public Map<String, Object> findAuthorityList(Long roleId){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<AuthorityPojo> authoritys = this.authorityService.findAuthorityList(roleId);
			map.put("authoritys", authoritys);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch (Exception e) {
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}	
		return map;
	}
	
	/**
	 * 通过ID查询权限
	 * @param authorityId
	 * @return Authority
	 */
	@ResponseBody
	@RequestMapping(value = "/info", method = RequestMethod.GET) 
	public Map<String, Object> info(Long id){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Authority authority = this.authorityService.info(id);
			map.put("authority", authority);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch (Exception e) {
			e.printStackTrace();
			map.put("operateMsg", Constant.FAIL_CODE);
		}	
		return map;
	}
	
	/**
	 * 批量删除
	 * @param accountId
	 * @return 
	 */
	@RequestMapping(value = "/delete") 
	@ResponseBody
	public Map<String, Object> delBatch(String ids){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			int delNum = this.authorityService.delBatch(ids);
			map.put("delNum", delNum);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch(Exception e){
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 保存或新增权�?
	 * @param account
	 * @return 
	 */
	@RequestMapping(value = "/save") 
	@ResponseBody
	public Map<String, Object> save(Authority authority){
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			this.authorityService.save(authority);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch(Exception e){
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	/**
	 * 保存资源权限
	 */
	@RequestMapping(value = "/saveResourceByAuthorityId") 
	@ResponseBody
	public Map<String, Object> saveResourceByAuthorityId(String multiSelectedValue, Long authorityId){
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			this.authorityService.saveResourceByAuthorityId(multiSelectedValue, authorityId);
			map.put("operateMsg", Constant.SUCCESS_CODE);
			
			//重新加载权限资源映射关系
			MyAuthResoMapping.setResourceMap(userDetailsService.loadResourcesAndAuthority());
			
		}catch(Exception e){
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getResourceByAuthority", method = RequestMethod.GET)  
	public Map<String, Object> getResourceByAuthority(Long authorityId){
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			String resources = this.authorityService.getResourceByAuthority(authorityId).toString();
			map.put("resources", resources);
			map.put("operateMsg", Constant.SUCCESS_CODE);
		}catch(Exception e){
			map.put("operateMsg", Constant.FAIL_CODE);
		}
		return map;
	}
}
